/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.services;

import com.kcp.ko.fmtnmlib.service.dao.HibernatePersistenceDAO;
import org.braintrain.domain.FileManageable;
import org.braintrain.domain.Media;
import org.braintrain.domain.MediaDirectory;
import org.braintrain.services.search.media.UniqueFileManageableCriteriaFactory;
import org.braintrain.services.search.media.UniqueMediaQueryFactory;

/**
 * The Media Service.  This Class defines the various functionality
 * commonly used when dealing with Media.
 * 
 * @author John Ericksen
 *
 */
public class MediaServiceImpl implements MediaService {
    
    private HibernatePersistenceDAO genericDAO;
    
    public HibernatePersistenceDAO getGenericDAO() {
        return genericDAO;
    }
    public void setGenericDAO(HibernatePersistenceDAO genericDAO) {
        this.genericDAO = genericDAO;
    }
    
    /**
     * Returns the appropriate media resolved (and unique) along a given
     * path. (see UniqueMediaQueryFactory)
     * 
     * @param path
     * @return
     */
    public Media getResolvedMedia(String path) {
        
        String fixedPath = path;
        if(fixedPath.startsWith("http") && fixedPath.contains("/braintrain/"))
        {
            fixedPath = fixedPath.substring(path.indexOf("/braintrain/"));
        }
        else if(fixedPath.startsWith("http"))
        {
            return null;
        }
        
        String directoryPath = fixedPath.substring(0, fixedPath.lastIndexOf('/') + 1);
        String name = fixedPath.substring(fixedPath.lastIndexOf('/') + 1);
        
        return genericDAO.findUnique(new UniqueMediaQueryFactory(name, directoryPath));
        
    }
    
    /**
     * Returns true of a given media name is unique in a directory.
     * (see UniqueFileManageableCriteriaFactory)
     * 
     * @return
     */
    public boolean getUniqueFileManageable(Long currentDirectoryId, String name) {
        MediaDirectory currentDirectory = null;
        if(currentDirectoryId != null)
        {
            currentDirectory = genericDAO.getById(MediaDirectory.class, currentDirectoryId);
        }
        FileManageable managable = genericDAO.findUnique(new UniqueFileManageableCriteriaFactory(name, currentDirectory));
        return managable == null;
    }
    
    /**
     * Returns the appropriate media in a directory.
     * (see UniqueFileManageableCriteriaFactory)
     * 
     * @return
     */
    public Media getUniqueMedia(Long currentDirectoryId, String name) {
        MediaDirectory currentDirectory = null;
        if(currentDirectoryId != null)
        {
            currentDirectory = genericDAO.getById(MediaDirectory.class, currentDirectoryId);
        }
        FileManageable manageable = genericDAO.findUnique(new UniqueFileManageableCriteriaFactory(name, currentDirectory));
        if(manageable instanceof Media)
        {
            return (Media)manageable;
        }
        return null;
    }
    
    
}
